package models

import (
	"demo/utils"
	"fmt"
	"github.com/astaxie/beego/orm"
)

type Users struct {
	Id	int
	Username	string
	Password	string
	Status      int
	Createtime	int64
}

func InsertUser(user Users) (int64, error)  {
	return utils.ModifyDB("insert into users(username,password,status,createtime) values (?, ?, ?, ?)", user.Username, user.Password, user.Status, user.Createtime)
}

func QueryUserWightCon(con string) int  {
	sql := fmt.Sprint("select id from users %s", con)
	fmt.Println(sql)
	row := utils.QueryRowDB(sql)
	id := 0
	row.Scan(&id)
	return id
}

func QueryUserWithUsername(username string) int  {
	sql := fmt.Sprint("where username = '%s'", username)
	return QueryUserWightCon(sql)
}

func QueryUserWithParam(username string, password string) int  {
	sql := fmt.Sprint("where username = '%s' and password = '%s'", username, password)
	return QueryUserWightCon(sql)
}

func (u *Users) GetUser() *Users {
	//orm.NewOrm().QueryTable()
	var userList []*Users
	//querySeter := ormObject.QueryTable("users").Filter("id", 1)
	//rows, err := querySeter.
	//	OrderBy("-update_at").
	//	All(&userList)
	//fmt.Println(err, rows)
	//if userList != nil && len(userList) > 0 {
	//	return userList[0]
	//}
	//return nil

	//o := orm.NewOrm()
	res,err := orm.NewOrm().QueryTable(u).Filter("id", 1).All(&userList)
	fmt.Println("QueryTable result：", res, err)
	fmt.Println("userList：", userList)
	return userList[0]
}


